package com.acme.bankapp.service.bank;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import com.acme.bankapp.domain.bank.AbstractAccount;
import com.acme.bankapp.domain.bank.AccountsCompare;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;

public class BankReport {
	
	public int getNbOfCustomers(Bank bank) {
		return bank.getNumClients();
	}
	
	public int getNbOfAccounts(Bank bank) {
		int sum = 0;
		Iterator <Client> iter = bank.getClients().iterator();
		while (iter.hasNext()) {
			sum += iter.next().getAccounts().size();
		}
		return sum;
	}
	
	public void getAccountsSortedBySum(Bank bank) {
		Iterator <Client> iter = bank.getClients().iterator();
		Set<AbstractAccount> sortAcc = new TreeSet<AbstractAccount>(new AccountsCompare());
		while (iter.hasNext()) {
			sortAcc.addAll(iter.next().getAccounts().values());
		}
		System.out.println(sortAcc);
	}
	
	public int getBankCredit(Bank bank) {
		int sum = 0;
		Iterator <Client> iter = bank.getClients().iterator();
		Iterator <AbstractAccount> accIt;
		Collection <AbstractAccount> accounts;
		while (iter.hasNext()) {
			accounts = iter.next().getAccounts().values();
			accIt = accounts.iterator();
			while(accIt.hasNext()) {
				double balance = accIt.next().getBalance();
				if (balance < 0) {
					sum += balance;
				}
			}
		}
		return sum;
	}
	
	public Map<Client, Collection<AbstractAccount>> getCustomerAccount(Bank bank) {
		Set<Client> clientSet = bank.getClients();
		Iterator <Client> iter = clientSet.iterator();
		Client client;	
		Map <Client, Collection<AbstractAccount>> clientMap = new HashMap<Client, Collection<AbstractAccount>>();
		Collection<AbstractAccount> clientAccounts;
		while (iter.hasNext()) {
			client = iter.next();
			clientAccounts = client.getAccounts().values();	
			clientMap.put(client, clientAccounts);
		}
		return clientMap;
	}

}
